package com.title2;

import java.io.*;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

/**
 * @Author milian
 * @Date 2021/8/4 0004 19:27
 */
public class title2 {
    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(System.in);
        System.out.println("1.普通字符流  2.字符缓冲流");
        System.out.println("请选择文件复制方式：");
        String choose = sc.next();
        ArrayList<String> arrayList = new ArrayList<>();
        switch (choose){
            case "1":
                arrayList= InfoScanner(sc);
                common(arrayList.get(0).toString(),arrayList.get(1).toString());
                break;
            case "2":
                arrayList= InfoScanner(sc);
                special(arrayList.get(0).toString(),arrayList.get(1).toString());
                break;
        }
    }

    private static void special(String src, String target) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader(src));
        BufferedWriter bw = new BufferedWriter(new FileWriter(target));
        String len;
        while ((len=br.readLine())!=null){
            bw.write(len);
        }
        br.close();
        bw.close();
    }

    private static void common(String src, String target) throws IOException {
        FileReader fr = new FileReader(src);
        FileWriter fw = new FileWriter(target);
        char[] chars = new char[1024];
        int len;
        while ((len=fr.read(chars))!=-1){
            fw.write(chars);
        }
        fr.close();
        fw.close();
    }

    public static ArrayList<String> InfoScanner(Scanner sc) {
        System.out.println("请输入数据源：");
        String srcPath = sc.next();
        System.out.println("请输入目的地：");
        String targetPath = sc.next();
        ArrayList<String> arrayList = new ArrayList<>();
        arrayList.add(srcPath);
        arrayList.add(targetPath);
        return arrayList;
    }
}
